<template>
  <div :class="{dingdanContent:this.$route.params.index == 0}">
    <ul class="goods-list" v-show="this.$route.params.index == 0">
      <li v-for="(item,index) in goodsList" :key="index">
        <img :src="item.gimg" alt />
        <p>
          <span>{{item.gname}}</span>
          <span>颜色：{{item.color}} 尺码：{{item.size}}</span>
        </p>
        <p>
          <span>￥{{item.gprice}}</span>
          <span>x{{item.gcount}}</span>
          <span @click="delGoods(item.gid,index,item.color,item.size)">取消订单</span>
        </p>
      </li>
    </ul>
    <p class="goods-null" v-show="this.$route.params.index != 0">{{goodsNull}}</p>
  </div>
</template>

<script>
import cart from "@/api/shopcart.js";
export default {
  data() {
    return {
      goodsNull: "暂时没有查到相应的订单",
      goodsList: []
    };
  },

  components: {},
  created() {
    this.getGoods();
  },
  methods: {
    async getGoods() {
      try {
        let p = await cart.getDingdan(this.$store.state.user.username);
        if (p.data.flag) {
          this.goodsList = p.data.data;
        }
      } catch (err) {
        this.$toast.fail(err + "");zui
      }
    },
    delGoods(gid, index, color, size) {
      this.$dialog.confirm({
          message: "确定要取消订单吗？"
        })
        .then(async () => {
          try {
            let p = await cart.delDingdan(this.$store.state.user.username,gid,color,size);
            if (p.data.flag) {
              this.goodsList.splice(index, 1);
            }
          } catch (err) {
            this.$toast.fail(err + "");
          }
        })
        .catch(() => {
          // on cancel
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.dingdanContent {
  background: rgb(238, 238, 238);
}
.goods-null {
  width: 100%;
  height: 200px;
  line-height: 200px;
  font-size: 16px;
}
.goods-list {
  li {
    width: 100%;
    height: 110px;
    background: #fff;
    margin-bottom: 10px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    p {
      height: 100%;
      display: flex;
      flex-direction: column;
      text-align: left;
      &:nth-of-type(1) {
        justify-content: space-around;
        align-items: flex-start;
        width: 45%;
        span:nth-of-type(1) {
          font-size: 14px;
        }
      }
      &:nth-of-type(2) {
        text-align: right;
        justify-content: space-around;
        align-items: flex-end;
        width: 15%;
        span:nth-of-type(1) {
          font-weight: bold;
          font-size: 14px;
        }
        span:nth-of-type(2) {
          font-size: 14px;
        }
        span:nth-of-type(3) {
          display: inline-block;
          text-align: center;
          line-height: 20px;
          width: 60px;
          height: 20px;
          background: rgba(184, 28, 33, 0.904);
          color: #fff;
          border-radius: 8px;
        }
      }
    }
    img {
      width: 26%;
      height: 90%;
      border-radius: 10px;
    }
  }
}
</style>